package com.mc.metaSearch;

import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SearchBlog {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SearchBlog.class);

	final String prefix = "http://www.google.com.hk/search?q=";

	final String postfix = "++inurl:blog&num=30";

	// "&hl=zh-CN&lr=lang_zh-CN%7Clang_zh-TW&newwindow=1&safe=strict&tbs=lr%3Alang_1zh-CN%7Clang_1zh-TW&aq=f&aqi=&aql=&oq=&gs_rfai=";

	public String[] getContent(String querystring) throws Exception {

		StringBuffer sb = new StringBuffer();
		sb.append(prefix).append(querystring).append(postfix);// "GBK"
		try {
			URL url = new URL(sb.toString());
		//	logger.info(sb.toString());
			HttpURLConnection con = (HttpURLConnection) url.openConnection();

			con.setRequestProperty("User-Agent",
					"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2)");
			con.connect();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					con.getInputStream(), "UTF-8")); // Unicode
			StringBuilder b = new StringBuilder();
			String line;
			while ((line = reader.readLine()) != null) {
				
				b.append(line);
			//	b.append("\r\n");
			}
		//   logger.info(b + "---content");
			Pattern p = Pattern
					.compile("<h3 class=\"r\">.*</div></li>");
			List<String> result = new ArrayList<String>();

			Matcher m = p.matcher(b.toString());
			if (m.find()) {
				String[] src = m.group().split("<li class=\"g\">");
				for (String i : src) {
					if (!(i.isEmpty() || i.equals("")))
						result.add(i);
				}
			}
			return (String[]) result.toArray(new String[result.size()]);
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
			return null;
		} catch (ConnectException ex) {
			ex.printStackTrace();
			return null;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}

	}
	
	private String downloadGoogleSouceHtml(URL pageUrl)
	{
		InputStream is;
		InputStreamReader isr;
		BufferedReader br;
		String souceHtml = "";
		String strTemp = "";
		
		int conNum = 0;
		while(conNum < 3){
			if(conNum >= 1)
			{
				try {
					Thread.sleep(30000);
				} catch (InterruptedException ie){
					System.out.println(ie);
				}
			}
			
			try
			{
				Thread.sleep(20000);
				HttpURLConnection urlcon = (HttpURLConnection)pageUrl.openConnection();
				urlcon.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2)");
				//urlcon.setFollowRedirects(true);
				//urlcon.setInstanceFollowRedirects(false);
				urlcon.connect();
				is = urlcon.getInputStream();
				isr = new InputStreamReader(is);
				br = new BufferedReader(isr);
				do
				{
					strTemp = br.readLine();
					souceHtml = souceHtml + strTemp;
				}while(strTemp != null);
				return souceHtml;
			 }
			catch(SocketTimeoutException e)
			{
				System.out.println("The " + pageUrl.toString() + "connection is overtime");
				conNum++;
				continue;
			}
			catch(IOException e)
			{
				 System.out.println("The "+pageUrl.toString()+" Can't connect");
				 conNum++;
				 continue;
			}				
			catch(Exception e)
			{
				System.out.println("The "+pageUrl.toString()+" Can't connect");
				conNum++;
				continue;
			}
		}
		
		return null;
	}

	public Map<Integer, String> getURL(String[] content) throws Exception {
		// "<p><a href=\"/url\\?q=.*<br><font color=\"green\">"
		Pattern p_url = Pattern.compile("<a href=\"/url\\?q=.*<div class=\"s\">");
		Map<Integer, String> urls = new HashMap<Integer, String>();
		int k = 0;
		for (String i : content) {

			Matcher m = p_url.matcher(i);
			if (m.find()) {
				String[] src = m.group().split("<a href=\"/url\\?q=");
				for (String url : src) {
					String url1 = url.replaceAll("&amp;sa=U&amp", "").replaceAll(";ei.*", "");
					if (!(url1.isEmpty() || url1.equals("")))
						urls.put(k, url1);
				}
			}
			k++;
		}
		return (urls);
	}

	public Map<Integer, String> getTitle(String[] content) throws Exception {

		Pattern p_title = Pattern.compile("target=\"_blank\">.*</a></h3>");
		Map<Integer, String> titles = new HashMap<Integer, String>();
		int k = 0;
		for (String i : content) {
			Matcher m = p_title.matcher(i);
			if (m.find()) {
				String[] src = m.group().split("target=\"_blank\">");

				for (String title1 : src) {
					String title = title1.replaceAll("\">", "")
							.replaceAll("<[^>]*>", "")
							.replaceAll("[(/>)<]", "");
					;
					if (!(title.isEmpty() || title.equals("")))
						titles.put(k, title);
				}
			}
			k++;
		}

		return (titles);
	}

	public Map<Integer, String> getAbstract(String[] content) throws Exception {
		Pattern p_abstract = Pattern
				.compile("<span class=\"st\".*</li>");
		Map<Integer, String> abstracts = new HashMap<Integer, String>();
		int k = 0;
		for (String i : content) {
			Matcher m = p_abstract.matcher(i);
			if (m.find()) {
				String[] src = m.group().split("<span class=\"f\">");

				for (String abstract1 : src) {
					String abstract_ = abstract1
							.replaceAll("span class=\"st\">", "")
							.replaceAll("<[^>]*>", "")
							.replaceAll("[(/>)<]", "");
					;
					if (!(abstract_.isEmpty() || abstract_.equals("")))
						abstracts.put(k, abstract_);
				}
			}
			k++;
		}

		return (abstracts);
	}

	static public void main(String[] args) throws Exception {
		SearchBlog g = new SearchBlog();
		String[] r;
		Map<Integer, String> urls;
		Map<Integer, String> abstracts;
		Map<Integer, String> titles;
		try {
			r = g.getContent("西藏自然科学博物馆");
			urls = g.getURL(r);
			abstracts = g.getAbstract(r);
			titles = g.getTitle(r);
			Iterator it = abstracts.keySet().iterator();
			int num = 0;
			String url = "";
			String title="",abstracts1="";
			while (it.hasNext()) {
				num = Integer.parseInt(it.next().toString());
				url = urls.get((Object) num);
				title=titles.get((Object) num);
				abstracts1=abstracts.get((Object) num);
				System.out.println(url+"     :  "+title+"      "+abstracts1);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
